﻿using System;

namespace Trentacular.SharePoint.StsAdmin
{
    public class BaseConsoleHelper
    {
        public int ErrorCount { get; private set; }

        public BaseConsoleHelper()
        {
            ErrorCount = 0;
        }

        protected void HandleError(Exception e, bool pad)
        {
            HandleErrorWorker(null, e, pad);
        }
        protected void HandleError(string header, Exception e, bool pad)
        {
            HandleErrorWorker(header, e, pad);
        }
        private void HandleErrorWorker(string header, Exception e, bool pad)
        {
            if (pad)
                Console.WriteLine();

            string message = "";
            if (!String.IsNullOrEmpty(header))
                message += header + ": ";
            message += e.Message;

            Console.WriteLine(message);

            if (pad)
                Console.WriteLine();

            ErrorCount++;
        }

        protected void DoConsoleAction(string description, Action action)
        {
            Console.Write(string.Format("{0} ... ", description));
            try
            {
                action();
                Console.WriteLine("Success");
            }
            catch (Exception e)
            {
                HandleError(e, false);
            }
        }
    }
}
